//
//  network.m
//  cwRadio
//
//  Created by Kenneth on 11/15/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "Network.h"
#import "playlist.h"
#import "songDetails.h"
//#import "TBXML.h"
//#import "NSDataAdditions.h"

@implementation Network

@synthesize name, connection, data;

- (void) dealloc{
	
	[name release];
	[connection release];
	[super dealloc];
}

- (void) zitikerwelingvd{
	NSLog(@":D:D:D:D:D:D:D:D:P");
}

- (bool) connect: (NSString*) _name{
	[self setName:_name];
	NSString *post = [[NSString alloc] initWithFormat:@"name=%@&pwd=%@",name, @"qwerty"];
	
	NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];  
	
	NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];  
	
	NSURL *url = [NSURL URLWithString:@"http://www.chakrainteractive.com/mob/iphone/login/chckusr.php"];
	NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
	[theRequest setHTTPMethod:@"POST"];  
	[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];  
	[theRequest setHTTPBody:postData];      
	
	
	NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
	
	if( theConnection )
	{
		data = [[NSMutableData data] retain];
	}
	else
	{
		
	}
	
	
	return true;
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
	[data setLength: 0];

}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)_data
{		
	[[self data] appendData:_data];
	[self parseData];
}

-(void) parseData{
	NSString* pathje = [[NSBundle mainBundle]pathForResource:@"qwert" ofType:@"txt"];
	NSString* ns=[NSString stringWithContentsOfFile:pathje];
	tbxml = [[TBXML tbxmlWithXMLString:ns] retain];
	TBXMLElement *root = [tbxml rootXMLElement];
	if(root!=nil){
		playlist* pl=[[playlist alloc]init];
		TBXMLElement *current= [TBXML childElementNamed:@"current" parentElement:root];
		if(current != nil){
			TBXMLElement * _id=[TBXML childElementNamed:@"id" parentElement:current];
			TBXMLElement * _name = [TBXML childElementNamed:@"name" parentElement:current];
			TBXMLElement * url= [TBXML childElementNamed:@"url" parentElement:current];
			TBXMLElement * stream= [TBXML childElementNamed:@"stream" parentElement:current];
			TBXMLElement * duration= [TBXML childElementNamed:@"duration" parentElement:current];
			TBXMLElement * album_name= [TBXML childElementNamed:@"album_name" parentElement:current];
			TBXMLElement * artist_name= [TBXML childElementNamed:@"artist_name" parentElement:current];
		 songDetails* sd= [[songDetails alloc]initWithArtist:[TBXML textForElement:artist_name]		
													   title: [TBXML textForElement:_name] 
													   album:[TBXML textForElement:album_name]	 
													   genre: rock
														 url: [TBXML textForElement:url]				
													  stream: [TBXML textForElement:stream] 
														_id:[[TBXML textForElement:_id]intValue]	
													duration:[[TBXML textForElement:duration]intValue]];
			[pl add:sd];
			TBXMLElement * track = [TBXML childElementNamed:@"track" parentElement:root];
			while(track!=nil){
				TBXMLElement * _id=[TBXML childElementNamed:@"id" parentElement:track];
				TBXMLElement * _name = [TBXML childElementNamed:@"name" parentElement:track];
				TBXMLElement * url= [TBXML childElementNamed:@"url" parentElement:track];
				TBXMLElement * stream= [TBXML childElementNamed:@"stream" parentElement:track];
				TBXMLElement * duration= [TBXML childElementNamed:@"duration" parentElement:track];
				TBXMLElement * album_name= [TBXML childElementNamed:@"album_name" parentElement:track];
				TBXMLElement * artist_name= [TBXML childElementNamed:@"artist_name" parentElement:track];
				songDetails* sd=[[songDetails alloc]initWithArtist:[TBXML textForElement:artist_name] 
															 title: [TBXML textForElement:_name]
															 album: [TBXML textForElement:album_name]
															 genre: rock
															   url: [TBXML textForElement:url]
															stream: [TBXML textForElement:stream] 
															   _id:[[TBXML textForElement:_id]intValue]
														  duration:[[TBXML textForElement:duration]intValue]];
				[pl add:sd];
				track=[TBXML nextSiblingNamed:@"track" searchFromElement:track];	
			}
		}else {
			return;
		}
	}
	[tbxml release];
}


@end
